package com.meepo.basic.usersettings

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
import com.meepo.basic.AppCoreModule
import com.meepo.basic.R
import com.meepo.framework.design.dialog.SweetAlertDialog
import com.meepo.framework.design.helper.StatusBarHelper
import com.meepo.sdk.utils.ActivityHelper
import com.meepo.basic.components.view.MeepoActivity
import com.meepo.basic.upgrader.Upgrader
import com.meepo.sdk.component.observer.ObserverManager
import com.meepo.sdk.utils.store.CacheDirUtils
import kotlinx.android.synthetic.main.settings_activity.*


/**
 * @author
 *
 * @date  2020/10/25 3:08 PM
 * @version 1.0
 */
class SettingsActivity : MeepoActivity() {



    override fun setUpContentLayout(): Int = R.layout.settings_activity

    override fun initData() {
        presenter.call(
            ObserverManager.applyFunc { UserSettingsStore.Func.fetchAndApplyUserSettings() },
            { app_settings.setSettingsItemGroup(UserSettingsStore.State.SETTINGS_GROUPS) }
        )
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {

//        ImmersionBar.with(this)
//            .statusBarColor(R.color.sweet_dialog_bg_color)     //状态栏颜色，不写默认透明色
//            .autoStatusBarDarkModeEnable(true,0.2f)
//            .init();

        StatusBarHelper.immerse(this, header_ll)
//        StatusBarHelper.setDarkMode(this)
//        StatusBarHelper.setMargin(this, header_ll)


        app_settings.setOnItemClickListener { item, _ ->
            when (item.key) {
                UserSettingsStore.SETTING_ITEM_NIGHT_MODE -> setUpNightMode()
                UserSettingsStore.SETTING_ITEM_FONT_SIZE -> setUpFontSize()
                UserSettingsStore.SETTING_ITEM_CLEAR_CACHE -> setUpClearCache()
                UserSettingsStore.SETTING_ITEM_CHECK_UPDATE -> setUpCheckUpdate()
            }
        }

        back_iv.setOnClickListener { finish() }

        AppCoreModule.getUserSummary()?.let {
            logout_btn_divider.visibility = View.VISIBLE
            logout_btn.visibility = View.VISIBLE

            logout_btn.setOnClickListener { AppCoreModule.logout();finish() }
        }

    }

    private fun setUpCheckUpdate() {
        Upgrader.tryUpgrade(this, true)
    }

    private fun setUpClearCache() {

        SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
            .setTitleText("")
            .setContentText("确定要清除所有缓存吗?")
            .setConfirmText("是的")
            .setCancelText("取消")
            .setConfirmClickListener {
                CacheDirUtils.clearAllCache(this)
                initData()
                it.cancel()
            }
            .show()
    }

    private fun setUpFontSize() {
        val singleChoiceDialog = AlertDialog.Builder(this)
        singleChoiceDialog.setTitle("字体大小")
        // 第二个参数是默认选项，此处设置为0
        // 第二个参数是默认选项，此处设置为0
        singleChoiceDialog.setSingleChoiceItems(
            UserSettingsStore.State.FONT_SIZE_OPTIONS, UserSettingsStore.State.userSettings.fontSize
        ) { dialog, which ->
            UserSettingsStore.Func.saveFontSize(which)
            initData()
        }
        singleChoiceDialog.setCancelable(true)
        singleChoiceDialog.setNegativeButton("取消") { dialog, which -> }
//        singleChoiceDialog.setPositiveButton("确定") { dialog, which -> }

        singleChoiceDialog.show()
    }

    private fun setUpNightMode() {
        ActivityHelper.launch(this@SettingsActivity, SettingsNightModeActivity::class.java)
    }
}